<!--<script src="../../router/index.js"></script>-->
<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      :immediate-check="false"
      @load="onLoad"
      loading-text="正在加载"
      finished-text="加载完成"
    >
      <van-cell v-for="data in datalist" :key="data.filmId" @click="handelclick(data.filmId)">
        <img :src="data.poster">
        <b>{{ data.name }}</b>
        <p style="overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;">主演:{{ data.actors | actorFilter }}</p>
        <p>{{ data.nation }} | {{ data.runtime }}分钟</p>

      </van-cell>
    </van-list>
  </div>
</template>

<script>
import Vue from 'vue'
import { List, Cell } from 'vant'
import http from '@/util/http'
import { mapState } from 'vuex'
Vue.use(List).use(Cell)
Vue.filter('actorFilter', (actors) => {
  if (actors === undefined) return '暂无主演'
  return actors.map(actors => actors.name).join(' ')
})
export default {
  data () {
    return {
      datalist: [],
      loading: false, // 当滑到底部时会自动更改为true
      finished: false,
      current: 1, // 记录第几页
      total: 0
    }
  },
  computed: {
    ...mapState('CityModule', ['cityId'])
  },
  mounted () {
    http({
      url: `/gateway?cityId=${this.cityId}&pageNum=1&pageSize=10&type=1&k=8743745`,
      headers: {
        'X-Host': 'mall.film-ticket.film.list'
      }
      // method: 'get'
    }).then(res => {
      this.datalist = res.data.data.films
      this.total = res.data.data.total
    })
  },
  methods: {
    onLoad () {
      if (this.datalist.length === this.total && this.datalist.length !== 0) {
        this.finished = true
        return
      }

      this.current++
      console.log('到底了')
      // 第一步，ajax请求新数据，第二步，合并新老数据，第三步，将loadinfg设置为false
      http({
        url: `/gateway?cityId=${this.cityId}&pageNum=${this.current}&pageSize=10&type=1&k=8743745`,
        headers: {
          'X-Host': 'mall.film-ticket.film.list'
        }
        // method: 'get'
      }).then(res => {
        // this.datalist = res.data.data.films
        this.datalist = [...this.datalist, ...res.data.data.films]

        this.loading = false
      })
    },
    handelclick (id) {
      // 编程式导航
      // this.$router.push(`/detail/${id}`)
      // 第二种方法 ，通过路由名字
      // this.$router.push({
      //   name:"kerwindetail",
      //   params:{
      //     myid:id
      //   }
      // })
      // 第三种方式，通过query方式携带参数
      this.$router.push(`/detail?id=${id}`)
    }
  }
}
</script>
<style scoped lang="scss">
.van-cell {
  overflow: hidden;
  padding: 10px;

  img {
    float: left;
    width: 100px;
  }
}

</style>
